//
//  MainViewController.m
//  ShareYOU
//
//  Created by 杨亭 on 14-12-22.
//  Copyright (c) 2014年 ShareYOU. All rights reserved.
//

#import "MainViewController.h"
#import "SettingViewController.h"
#import "BillViewController.h"
#import "DataBaseHandler.h"
#import "MainCell.h"

@interface MainViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain)DataBaseHandler *dataHandler;

@end

@implementation MainViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        //单例的使用
        self.dataHandler = [DataBaseHandler shareInstance];
        self.dataArray = [NSMutableArray array];
        [_dataHandler openDB];
        //创建表成功
        [_dataHandler createTable];
        //创建表成功
        [_dataHandler createPersonnelTable];
        
        
    }
    return self;
}





//添加账单页面
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    CGFloat width = self.view.frame.size.width / 4;
    CGFloat height = self.view.frame.size.height / 2 ;
    
    
    //设置背景图片
//    self.view.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"backGroundShow.png"]];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
//    UIView *viewTable = [[UIView alloc] initWithFrame:CGRectMake(0, height, width * 4, height - 44)];
//    
//    viewTable.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"firstShow.png"]];
//    [self.view addSubview:viewTable];
//    [viewTable release];
    
//    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 375, height)];
//    imageView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"firstShow.png"]];
//    [self.view addSubview:imageView];
//    [imageView release];
    
    //view显示
    //按钮记一笔
    self.billButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.billButton.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"add.png"]];
    self.billButton.frame = CGRectMake(width, height * 3 / 4, 144, 36);
//    self.billButton.layer.masksToBounds = YES;
//    self.billButton.layer.cornerRadius = 50;
    [self.billButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [self.billButton setTitle:@"记一笔" forState:UIControlStateNormal];
    [self.billButton addTarget:self action:@selector(billButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.billButton];
    
    //显示文字
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 193, 65)];
//    label1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"showAccunt.png"]];
    label1.text = @"快来记一笔吧";
    [self.view addSubview:label1];
    [label1 release];
    
//    UILabel *label2 = [[UILabel alloc] initWithFrame:CGRectMake(120, 110, 100, 20)];
//    label2.text = @"金持记账";
//    [self.view addSubview:label2];
//    [label2 release];
//    
    //创建tableView
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, height, width * 4, height - 50)];
    self.tableView.backgroundColor = [UIColor clearColor];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.separatorStyle = NO;
//    self.tableView.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"backGroundShow.png"]];
    [self.view addSubview:self.tableView];
    [_tableView release];
    
//    self.dataArray = [self.dataHandler selectAll];
    NSLog(@"-------------------%@", self.dataArray);
    // 创建一个手势识别器
    UISwipeGestureRecognizer *recognizer;
    
    //右滑
    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
    
    [recognizer setDirection:(UISwipeGestureRecognizerDirectionRight)];
    
    [[self view] addGestureRecognizer:recognizer];
    
    
//    //左滑
//    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
//    
//    [recognizer setDirection:(UISwipeGestureRecognizerDirectionLeft)];
//    
//    [[self view] addGestureRecognizer:recognizer];
//    
//    
//    //上滑
//    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
//    
//    [recognizer setDirection:(UISwipeGestureRecognizerDirectionUp)];
//    
//    [[self view] addGestureRecognizer:recognizer];
//
//    //下滑
//    recognizer = [[UISwipeGestureRecognizer alloc]initWithTarget:self action:@selector(handleSwipeFrom:)];
//    
//    [recognizer setDirection:(UISwipeGestureRecognizerDirectionDown)];
//    
//    [[self view] addGestureRecognizer:recognizer];

    [self.tableView reloadData];

    //开启编辑
    self.navigationItem.rightBarButtonItem = self.editButtonItem;
    
    //创建添加按钮
    self.navigationItem.hidesBackButton=YES;
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"添加"  style:UIBarButtonItemStylePlain target:self action:@selector(addAction)];//UIBarButtonItem
}

//手势方法的实现
-(void)handleSwipeFrom:(UISwipeGestureRecognizer *)recognizer{
    
//    //往左滑实现
//    if(recognizer.direction==UISwipeGestureRecognizerDirectionLeft) {
//        
//        DisplayViewController *displayVC = [[DisplayViewController alloc] init];
//        CATransition*transition=[CATransition animation];
//        transition.duration=0.3;
//        transition.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
//        transition.type=kCATransitionPush;
//        transition.subtype=kCATransitionFromRight;
//        
//        [self.navigationController.view setBackgroundColor:[UIColor colorWithRed:0.0 green:156.0/255 blue:181.0/255 alpha:1]];
//        [self.navigationController.view.layer addAnimation:transition forKey:nil];
//        
//
//        [self.navigationController pushViewController:displayVC animated:NO];
//    }
//    
    //往右滑实现
    if(recognizer.direction==UISwipeGestureRecognizerDirectionRight) {
        SettingViewController *settingVC = [[SettingViewController alloc] init];
        CATransition*transition=[CATransition animation];
        transition.duration=0.3;
        transition.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
        transition.type=kCATransitionPush;
        transition.subtype=kCATransitionFromLeft;
        
        [self.navigationController.view setBackgroundColor:[UIColor colorWithRed:0.0 green:156.0/255 blue:181.0/255 alpha:1]];
        [self.navigationController.view.layer addAnimation:transition forKey:nil];

        [self.navigationController pushViewController:settingVC animated:NO];
        
        //执行程序
        
    }
    
}



//添加按钮方法的实现
-(void)addAction{
    SettingViewController *settingVC = [[SettingViewController alloc] init];
    CATransition*transition=[CATransition animation];
    transition.duration=0.3;
    transition.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type=kCATransitionPush;
    transition.subtype=kCATransitionFromLeft;
    
    [self.navigationController.view setBackgroundColor:[UIColor colorWithRed:0.0 green:156.0/255 blue:181.0/255 alpha:1]];
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    
    [self.navigationController pushViewController:settingVC animated:NO];
}



//屏幕旋转(防止屏幕倒置)
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}



//高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 100;
}



//有多少行cell
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    NSLog(@"%ld", self.dataArray.count);
//    [self.dataArray reloadData];
    return self.dataArray.count;
    
}

//- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
//{
//    //#warning Potentially incomplete method implementation.
//    // Return the number of sections.
//    return 1;
//}

//重用池
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    NSLog(@"--------------%@", self.dataArray);

    MainCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MainCell"];
    if (cell == nil) {
        cell = [[[MainCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"MainCell"] autorelease];
    }
    
//    NSLog(@"****************%@", self.dataArray);

    Payout *pay = [self.dataArray objectAtIndex:indexPath.row];
    cell.amountShow.text = pay.amount;
    cell.dateShow.text = pay.date;
    cell.typeShow.text = pay.type;
    cell.commentShow.text = pay.comment;
    cell.personnelShow.text = pay.personnel;
    
//    self. = [payArr lastObject];
    
//    [self.tableView reloadData];

//    cell.found = act;
//    
//    cell.myImageView.frame = CGRectMake(239, 33, 103, 128);
    return cell;

   
}

////点击cell
//-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    //点击后恢复原状
//    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//
//}





////点击删除调用的方法
//- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
//{
//    if (editingStyle == UITableViewCellEditingStyleDelete) {
//        //删除前对数据源处理
//        NSString *key = [self.dataArray objectAtIndex:indexPath.section];
//        NSMutableArray *stuArray = [self.dataArray objectForKey:key];
//        [stuArray removeObjectAtIndex:indexPath.row];
//        
//        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationTop];
//        
//        if (stuArray.count == 0) {
//            [self.dataArray removeObject:key];
//            
//            NSIndexSet *set = [NSIndexSet indexSetWithIndex:indexPath.section];
//            [tableView deleteSections:set withRowAnimation:UITableViewRowAnimationAutomatic];
//        }
//        
//    }
//}




//按钮记一笔方法的实现
- (void)billButtonAction:(UIButton *)button
{
    //从下面往上出得效果
    BillViewController *billVC = [[BillViewController alloc] init];
    CATransition *transition=[CATransition animation];
    transition.duration=0.5;
    transition.timingFunction=[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type=kCATransitionPush;
    transition.subtype=kCATransitionFromTop;
    
    [self.navigationController.view setBackgroundColor:[UIColor whiteColor]];
    [self.navigationController.view.layer addAnimation:transition forKey:nil];

    [self.navigationController pushViewController:billVC animated:NO];
}


//刷新数据
-(void)viewWillAppear:(BOOL)animated{
    self.dataArray = [NSMutableArray array];
    //查找数据
    self.dataArray = [self.dataHandler selectAll];
    [self.tableView reloadData];
    
}


//释放内存
- (void)dealloc
{
    [_tableView release];
    [_dataArray release];
    [super dealloc];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
